import QtQuick 1.0

QtObject {
    id: root
    property string source

    property int year
    property int month
    property int day
    property int hour
    property int minute
    property int second
    property int tz_hour
    property int tz_minute

    onMonthChanged: {
        if (root.month < 1) {
            root.year--
            root.month += 12
        }
        if (root.month > 12) {
            root.year++
            root.month -= 12
        }
    }

    onDayChanged: {
        if (root.day < 1) {
            root.month--
            root.day += daysOfMonth(root.year, root.month)
        }
        if (root.day > daysOfMonth(root.year, root.month)) {
            var year = root.year
            var month = root.month
            root.month++
            day -= daysOfMonth(year, month)
        }
    }

    onHourChanged: {
        if (root.hour < 0) {
            root.day--
            root.hour += 24
        }
        if (root.hour >= 24) {
            root.day++
            root.hour -= 24
        }
    }

    onMinuteChanged: {
        if (root.minute < 0) {
            root.hour--
            root.minute += 60
        }
        if (root.minute >= 60) {
            root.hour++
            root.minute += 60
        }
    }

    onSecondChanged: {
        if (root.second < 0) {
            root.minute--
            root.second += 60
        }
        if (root.second >= 60) {
            root.minute++
            root.second += 60
        }
    }

    function toLocalString() {
        var ret = "";
        if (root.year > 0) {
            ret += root.year;
            ret += "/";
            if (root.month < 10) ret += "0";
            ret += root.month;
            ret += "/";
            if (root.day < 10) ret += "0";
            ret += root.day;
            ret += " ";
            if (root.hour < 10) ret += "0";
            ret += root.hour;
            ret += ":";
            if (root.minute < 10) ret += "0";
            ret += root.minute;
        }
        return ret;
    }

    function daysOfMonth(year, month) {
        var ret = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
        if (month == 2 && ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ret++;
        return ret;
    }

    function toUTCString() {
        var ret = "";
        var component = Qt.createComponent("DateTime.qml");
        if (component.status == Component.Ready) {
            var utc = component.createObject(root);
            utc.year = root.year;
            utc.month = root.month;
            utc.day = root.day;
            utc.hour = root.hour - root.tz_hour;
            utc.minute = root.minute - root.tz_minute;
            utc.second = root.second;
            ret += utc.year;
            if (utc.month < 10) ret += "0";
            ret += utc.month;
            if (utc.day < 10) ret += "0";
            ret += utc.day;
            ret += "T";
            if (utc.hour < 10) ret += "0";
            ret += utc.hour;
            if (utc.minute < 10) ret += "0";
            ret += utc.minute;
            if (utc.second < 10) ret += "0";
            ret += utc.second;
            ret += "Z"
        }
        return ret;
    }

    onSourceChanged: {
        if (source.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})([\+\-])([0-9]{2}):([0-9]{2})$/)) {
            root.year = parseInt(RegExp.$1, 10);
            root.month = parseInt(RegExp.$2, 10);
            root.day = parseInt(RegExp.$3, 10);
            root.hour = parseInt(RegExp.$4, 10);
            root.minute = parseInt(RegExp.$5, 10);
            root.second = parseInt(RegExp.$6, 10);
            root.tz_hour = parseInt(RegExp.$8, 10) * (RegExp.$7 == "+" ? 1 : -1);
            root.tz_minute = parseInt(RegExp.$9, 10);
//            console.debug(root.source, root.year, root.month, root.day, root.hour, root.minute, root.second, root.tz_hour, root.tz_minute);
        } else {
            console.debug(root.source)
        }
    }
}
